package com.tianhui.consignortwofei.mvp.ui.activity.audit.driver;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.fgs.common.CommonResponse;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.manager.EventBusManage;
import com.tianhui.consignortwofei.mvp.model.DriverModel;
import com.tianhui.consignortwofei.mvp.model.enty.DriverInfo;
import com.tianhui.consignortwofei.mvp.ui.activity.audit.BaseAuditActivity;
import com.xuexiang.xui.widget.dialog.materialdialog.DialogAction;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/12/25
 * @description
 **/
public abstract class BaseDriverAuditActivity extends BaseAuditActivity {

    @BindView(R.id.layout_audit_bottom_saveButton)
    Button mSaveButton;
    @BindView(R.id.layout_audit_bottom_nextButton)
    Button mNextButton;
    protected DriverInfo mDriverInfo;
    protected DriverModel mDriverModel;


    @Override
    public String getToolbarTitle() {
        return "司机信息审核";
    }

    @Override
    public String getToolbarSubtitle() {
        return "返回司机列表";
    }

    @Override
    public void onSubtitleClick() {
        super.onSubtitleClick();
        new MaterialDialog.Builder(this)
                .content("确认要退出当前操作吗？")
                .positiveText("是")
                .negativeText("否")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        openActivity(DriverListActivity.class);
                    }
                })
                .show();
    }

    @Override
    public void init() {
        mDriverModel = new DriverModel();
        Bundle bundle = getIntent().getExtras();
        if (bundle != null && !bundle.isEmpty()) {
            String driverId = bundle.getString(KeyValue.KEY_ID);
            int operate = bundle.getInt(KeyValue.KEY_OPERATE);
            showOperateView(operate);
            if (!TextUtils.isEmpty(driverId)) {
                requestDriverInfo(driverId);
            } else {
                DriverInfo info = bundle.getParcelable(KeyValue.KEY_DRIVER_INFO);
                if (info != null) {
                    mDriverInfo = info;
                    showDriverInfo(info);
                }
            }

        }


    }

    protected void showOperateView(int operate) {
        if (operate == KeyValue.KEY_EDIT_OPERATE) {
            mSaveButton.setVisibility(View.VISIBLE);
        } else if (operate == KeyValue.KEY_AUDIT_OPERATE) {
            mSaveButton.setVisibility(View.GONE);
        }
    }

    private void requestDriverInfo(String driverId) {

        Map<String, String> map = new HashMap<>();
        map.put("id", driverId);
        mDriverModel.driverInfoModel(this, map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<DriverInfo>>() {
            @Override
            public void onNext(CommonResponse<DriverInfo> commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    DriverInfo info = commonResponse.getData();
                    if (info != null) {
                        mDriverInfo = info;
                        showDriverInfo(info);
                    }
                } else {
                    showToast(commonResponse.getMsg());
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });

    }

    private void requestDriverInfoSave(boolean isNeedAudit, DriverInfo info) {

        mDriverModel.driverInfoSaveModel(this, info, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    if (isNeedAudit) {
                        requestDriverAudit(info);
                    } else {
                        showToast(commonResponse.getMsg());
                    }
                    saveInfoSuccess();
                } else {
                    showToast(commonResponse.getMsg());
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });

    }

    private void requestDriverAudit(DriverInfo info) {
        Map<String, String> map = new HashMap<>();
        map.put("id", info.id);
        map.put("isaudit", "1");
        mDriverModel.driverAuditModel(this, map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    EventBusManage.refreshDriverList();
                    openActivity(DriverListActivity.class);
                } else {
                    showToast(commonResponse.getMsg());
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });

    }

    public void requestDriverSaveAndAudit(DriverInfo info) {
        mDriverModel.driverInfoSaveAndAuditModel(this, info, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    EventBusManage.refreshDriverList();
                    openActivity(DriverListActivity.class);
                } else {
                    showToast(commonResponse.getMsg());
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });

    }

    public abstract void showDriverInfo(DriverInfo info);

    public abstract DriverInfo getSaveInfo();

    public abstract void nextAudit();

    public void saveInfoSuccess() {

    }


    @OnClick(R.id.layout_audit_bottom_saveButton)
    public void save() {
        saveInfo(false);
    }

    public void saveInfo(boolean isNeedAudit) {
        DriverInfo info = getSaveInfo();
        if (info != null)
            requestDriverInfoSave(isNeedAudit, info);
    }

    @OnClick(R.id.layout_audit_bottom_nextButton)
    public void next() {
        nextAudit();
    }

    @Override
    public String getPictureModule() {
        return "driver";
    }

    @Override
    public void finish() {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putParcelable(KeyValue.KEY_DRIVER_INFO, mDriverInfo);
        intent.putExtras(bundle);
        setResult(KeyValue.KEY_RESULT_CODE, intent);
        super.finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == KeyValue.KEY_RESULT_CODE) {
            Bundle bundle = data.getExtras();
            if (bundle != null && !bundle.isEmpty()) {
                DriverInfo info = bundle.getParcelable(KeyValue.KEY_DRIVER_INFO);
                if (info != null) {
                    mDriverInfo = info;
                }
            }
        }
    }
}
